package com.atguigu.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

/**
 * Date:2021/3/9
 * Author:ybc
 * Description:
 */
public class ThirdServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.处理post请求的编码
        request.setCharacterEncoding("UTF-8");
        //2.获取请求参数，浏览器发送请求所携带的请求参数的格式为name=value&name=value
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //若出现多个重名的请求参数，需要使用request.getParameterValues("name")，获取所有的value所组成的数组
        String[] hobbies = request.getParameterValues("hobby");
        System.out.println("username:"+username+",password:"+password+",hobby:"+ Arrays.toString(hobbies));
        //转发
        //request.getRequestDispatcher("/success.html").forward(request, response);
        /**
         * 响应浏览器数据
         * 可以将数据直接作为响应体响应到浏览器，因此在页面中将直接显示响应的数据
         * 若不设置响应到浏览器的内容格式，浏览器会将内容作为文本处理，因此不会解析html标签
         * 需要通过response.setContentType("text/html");设置内容格式
         * 将响应到浏览器的数据当做以文本形式存在的html内容进行处理，此时html标签就会被解析
         * 解决响应数据的乱码，首先需要设置response对象的编码，使response以UTF-8的编码封装数据
         * 然后设置响应到浏览器数据的解码方式
         */
        /*response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        writer.write("helloworld<br>");
        writer.print("呵呵");*/
        //重定向
        response.sendRedirect("success.html");
    }
}
